<template>
<!--  <div>
    我是getWxCode

    https://bjyoupei.techbz.cn/dist/#/getWxCode
  </div>-->
</template>

<script>

import { myGet } from '@/utils/myRequest'
import wx from 'weixin-js-sdk'
export default {
  name: 'getWxCode',

  created() {

  },

  methods:{
    getUrlCode() { // 截取url中的code方法
      var url = location.search
      //this.winUrl = url
      var theRequest = new Object()
      if (url.indexOf("?") != -1) {
        var str = url.substr(1)
        var strs = str.split("&")
        for(var i = 0; i < strs.length; i ++) {
          theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1])
        }
      }
      return theRequest
    },
  },

  mounted() {

    const code =  this.getUrlCode().code
    const state =  this.getUrlCode().state
    //const code = this.$route.query.code;//微信返回的code
    //const state = this.$route.query.state;//微信返回的state
    console.log(code,state,location.search)
    //这个页面会运行在 小程序的webview，能否把当前本地缓存的用户id给webview，这样我可以直接去更新用户
    //如果不行，那就只能发起
    //const id = '';
    const url = "https://bjyoupei.techbz.cn/api/index/updatePublicOpenId"
    myGet(url,{code,state}).then(res=>{
      //wx.miniProgram.redirectTo({url: "/pages/index/index"})
      wx.miniProgram.switchTab({url: "/pages/index/index"})
    })
  }
}
</script>
